import time
import requests
import json
from datetime import datetime
# 使用的模型为qwen2.5  32B  一次判断一条记录 GPU消耗是90% 功耗 280W

class DataChecker:
    def __init__(self, url, model):
        self.url = url
        self.model = model

    def send_request(self, content):
        ai_content =""
        """
        向指定的API发送POST请求，并检查返回的消息中是否包含特定关键词。

        :param content: 用户输入的内容
        """
        data = {
            "model": self.model,
            "messages": [
                {
                    "role": "system",
                    #"content": "你作为一个数据核查人员 输入的数据中如果有 发送数据给NCC  这的关键词 关键词精确匹配 不要做推理 打印全部数据数据 如果没有就打印NO "
                    "content": "你作为一个数据核查人员 从输入的行中查找 发送数据给NCC 这个关键字找到后打印整行的内容 如果找不到输出 NO  "
                },
                {
                    "role": "user",
                    "content": content
                }
            ],
            "stream": False
        }

        try:
            response = requests.post(self.url, json=data)
            if response.status_code == 200:
                response_data = response.json()
                if 'message' in response_data and 'content' in response_data['message']:
                    ai_content  = response_data['message']['content']
                    # print(ai_content)
                    return ai_content 
                else:
                    print("响应数据中没有找到content字段")
            else:
                print(f"请求失败，状态码: {response.status_code}")
                return {response.status_code}
        except Exception as e:
            print(f"发生错误: {e}")
        return  ai_content    

    def read_and_process_file(self, file_path):
        """
        读取文件内容，每次处理10行内容，然后等待1秒钟。

        :param file_path: 文件的路径
        """
        start_time1 = datetime.now()
        with open(file_path, 'r', encoding='utf-8') as file:
            lines = file.readlines()
            total_lines = len(lines)
            strline10 = ""
            for i in range(0, total_lines, 10):
                for line in lines[i:i + 10]:
                    strline10 += line.strip()+"\n"
                time.sleep(1)
                start_time = datetime.now()
                current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                print(f"{current_time}|发送数据给AI|")
                 # 开始发送数据给AI做判断
                ai_content=self.send_request(strline10)
                end_time = datetime.now()
                elapsed_time = (end_time - start_time).total_seconds()
                current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                print(f"{current_time}|AI返回数据|"+ai_content)
                print(f"{current_time}|该次处理总耗时: {elapsed_time:.2f} 秒")
                print("\n\n")
                strline10 = ""





        end_time1 = datetime.now()          
        elapsed_time = (end_time1 - start_time1).total_seconds() 
        print(f"{current_time}|全部业务处理总耗时: {elapsed_time:.2f} 秒")

# 使用示例
if __name__ == "__main__":
    url = "http://172.16.0.9:11434/api/chat"
    model = "qwen2.5:14b-instruct"
    file_path = 'data1'

    checker = DataChecker(url, model)
    checker.read_and_process_file(file_path)
